package com.oc.mba.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.oc.mba.entity.DeviceMessageEntity;
import com.oc.mba.entity.MessageEntity;
import com.oc.mba.entity.OcDevices;
import com.oc.mba.service.DeviceService;
import com.oc.mba.service.MessageService;
import com.oc.mba.util.CommConsts;
import com.oc.mba.util.CommLog;
import com.oc.mba.util.JsonUtil;
import com.oc.mba.util.ResultCode;

@Controller
public class MessageController extends BaseController implements IController {

	private static final CommLog logger = new CommLog(MessageController.class);
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private DeviceService deviceService;
	
	
	@Override
	public String progress(WrappedReq wReq, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.info("request for message. req=" + wReq);
		
		if (!wReq.getContent().containsKey(CommConsts.DEVICE_ID)) {
			logger.info("invalid request, req doesn't contain deviceID flag.");
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.INVALID_REQ);
			result.put(CommConsts.Result_DETAIL_FLAG, "Less Param");
			return JsonUtil.jsonMTS(result);
		}
		
		Integer deviceID = wReq.getContent().getInt(CommConsts.DEVICE_ID);
		OcDevices device = deviceService.findById(deviceID);
		// 无效设备
		if (device == null) {
			logger.info("invalid device, device doesn't sign in database.");
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.FAILURE);
			result.put(CommConsts.Result_DETAIL_FLAG, "Invalid Device");
			return JsonUtil.jsonMTS(result);
		}
		
		// 取得所有公共消息列表 400
		if (wReq.getSignID().equals(CommConsts.SIGNID_LIST_MESSAGES)) {
			logger.info("get all message.");
			
//			messageService.createDailyMessage(device);
			
			List<MessageEntity> messageList = messageService.listAllMessage();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
			result.put("messages", messageList);
			
			List<DeviceMessageEntity> deviceMessageList = messageService.listAllDeviceMessage(device.getId());
			result.put("deviceMessages", deviceMessageList);
			return JsonUtil.jsonMTS(result);
		}
		// 确认消息 401
		else if (wReq.getSignID().equals(CommConsts.SIGNID_FEED_BACK_MESSAGES)) {
			logger.info("feedback message.");
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
			return JsonUtil.jsonMTS(result);
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
		return JsonUtil.jsonMTS(result);
	}

}
